<?php

$page = "Entrance";
$welcome = "";


include_once("common.php");

// If the user is attempting to log in, we need to do that before we reach
// the point where we check authentication.
function gpp_check_main_submission()
{
	if(isset($_GET['logout']))
		return "You have been logged out. Thanks for using greenPaperPlus!";
	
	if(!isset($_POST['submitted']))
		return "";
	
	// leave the function if there hasn't been both a username and password submitted.
	if(!isset($_POST['email']) or !isset($_POST['password']))
		return "You must enter both a username and a password.";
	
	if(isset($_POST['create_account']))
		return gpp_create_account();
		
	if(isset($_POST['login']))
		return gpp_login();
	
	return "An error has occured in form submission.";
}

$message = gpp_check_main_submission();

// If the user is logged in, we want to send them to the page where
// they can actually use the application.
if(gpp_is_authenticated())
{
	if(isset($_POST['create_account']))
	{
		gpp_redirect("profile");
	}
	gpp_redirect("main");
}

gpp_print_header($page, $welcome);

// Since we have printed the header, we are already in the <body> statement with a greeting
// above.

?>

<p>To log in, enter the e-mail and password that you used when you created your account. Then, press the Login button.</p>

<p>If you want to create an account, enter the e-mail and password you want to log in with, and press the Create Account button. You will prompted to confirm your password, and then your account will be created.</p>

<h2><?php echo $message ?></h2>

<div id="login_form">
	<form method="POST" name="login" action="index.php">
	<div id="login_labels">
	<label for="username">E-mail address: </label><br />
	<label for="password">Password:</label><br />
	Action:
	</div>
	
	<div id="login_boxes">
	<input type="hidden" name="submitted" value="true" />
	<input type="text" name="email" id="email" /><br />
	<input type="password" name="password" id="password" /><br />
	<input type="submit" name="login" value="Login" /><input type="submit" id="create_account" name="create_account" value="Create Account" />
	</div>
	</form>
</div>

<?php 

gpp_print_footer();

?>